package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.model.GetDictionaryList.RqBusNumList;
import com.emm.yixun.mobile.ui.customer.Refresh;

/**
 * 
 * <p>
 * <b>Title：</b> 选择列表适配器
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * 
 * @author tangxin
 * @date 2017年2月20日 上午10:19:14
 */
public class MultiInterfaceAdapter extends BaseAdapter {

	Activity a;

	ArrayList<RqBusNumList> list;

	/** 1【生活服务】2【生活配套】3【乐于参加的活动类型】4【兴趣爱好】5【日常信息获取途径】 */
	int MultiInterfaceType;

	/** 是否选择 1 选择列表 2 修改界面列表 3详情界面列表 */
	int isVisible;

	public MultiInterfaceAdapter(Activity a, ArrayList<RqBusNumList> LifeService, int MultiInterfaceType) {
		this.a = a;
		this.list = LifeService;
		this.MultiInterfaceType = MultiInterfaceType;
	}

	public void NotifyData(ArrayList<RqBusNumList> LifeService) {
		this.list = LifeService;
		notifyDataSetChanged();
	}

	public void SetIsVisible(int isVisible) {
		this.isVisible = isVisible;
		notifyDataSetChanged();
	}

	public String GetID(int MultiInterfaceType, int position) {
		return (null == list ? null : list.get(position).getDictionaryId());
	}

	@Override
	public int getCount() {
		return (null == list ? 0 : list.size());
	}

	@Override
	public Object getItem(int position) {
		return (null == list ? null : list.get(position));
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHoudler v = null;
		if (null == convertView) {
			convertView = LayoutInflater.from(a).inflate(R.layout.attst_item_multi, null);
			v = new ViewHoudler();
			v.select_icon = (ImageView) convertView.findViewById(R.id.select_image_att);
			v.select_name = (TextView) convertView.findViewById(R.id.spinner_name);
			v.attention_item_lines = convertView.findViewById(R.id.attention_item_lines);
			v.count_text = (TextView) convertView.findViewById(R.id.count_text);
			v.delete_btn = (ImageView) convertView.findViewById(R.id.delete_btn);
			convertView.setTag(v);
		} else {
			v = (ViewHoudler) convertView.getTag();
		}

		if (isVisible == 1) {
			v.select_icon.setVisibility(View.VISIBLE);
			v.count_text.setVisibility(View.GONE);
			v.delete_btn.setVisibility(View.GONE);
		} else if (isVisible == 2) {
			v.select_icon.setVisibility(View.GONE);
			v.count_text.setVisibility(View.VISIBLE);
			v.delete_btn.setVisibility(View.VISIBLE);
		} else {
			v.select_icon.setVisibility(View.GONE);
			v.count_text.setVisibility(View.VISIBLE);
			v.delete_btn.setVisibility(View.GONE);
		}

		v.delete_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				list.get(position).setSelect(false);
				Refresh.Refreshonl();
			}
		});

		SetView(v.select_name, v.select_icon, list.get(position).isSelect(), list.get(position).getDictionaryName());
		SetLines(list.size(), position, v.attention_item_lines);

		return convertView;
	}

	private void SetLines(int size, int position, View v) {
		if ((size - 1) == position && (isVisible == 2 || isVisible == 3)) {
			v.setVisibility(View.VISIBLE);
		} else {
			v.setVisibility(View.GONE);
		}
	}

	public void SetView(TextView v, ImageView image, boolean select, String strNmae) {
		if (select) {
			image.setImageResource(R.drawable.ico_selt);
		} else {
			image.setImageResource(R.drawable.ico_delt_no);
		}
		v.setText(strNmae);
	}

	public static class ViewHoudler {

		ImageView select_icon;

		TextView select_name;

		View attention_item_lines;

		TextView count_text;

		ImageView delete_btn;

	}

}
